package com.itcam.tingshu.account.receiver;

import com.itcam.tingshu.account.service.UserAccountService;
import com.itcam.tingshu.common.constant.KafkaConstant;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
public class AccountReceiver {

    @Autowired
    private UserAccountService userAccountService;

    /**
     * 监听消息实现用户账户初始化
     * @param record
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_USER_REGISTER) // 监听消息器
    public void userRegister(ConsumerRecord<String, String> record) {
        //  获取发送消息
        String userId = record.value();
        if (!StringUtils.isEmpty(userId)) {
            //  调用服务层方法
            userAccountService.initUserAccount(userId);
        }
    }

    /**
     * 扣减锁定金额
     * @param record
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_ACCOUNT_MINUS)
    public void minus(ConsumerRecord<String, String> record) {
        // 获取发送的数据
        String orderNo = record.value();
        // 判断
        if (!StringUtils.isEmpty(orderNo)) {
            userAccountService.minus(orderNo);
        }
    }

    /**
     * 监听消息实现用户账户解锁
     * @param record
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_ACCOUNT_MINUS)
    public void accountUnlock(ConsumerRecord<String, String> record) {
        // 获取发送的数据
        String orderNo = record.value();
        // 判断
        if (!StringUtils.isEmpty(orderNo)) {
            // 调用服务层方法
            userAccountService.accountUnlock(orderNo);
        }
    }

    /**
     * 监听消息实现用户账户充值
     * @param record
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_RECHARGE_PAY_SUCCESS)
    public void rechargePaySuccess(ConsumerRecord<String,String> record){
        // 获取发送消息
        String orderNo = record.value();
        if (!org.springframework.util.StringUtils.isEmpty(orderNo)){
            // 调用服务层方法.
            userAccountService.rechargePaySuccess(orderNo);
        }
    }
}